package com.openbravo.data.gui;

import com.openbravo.data.loader.LocalRes;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;

public class JImageEditor extends JPanel
{
  private Dimension m_maxsize;
  private ZoomIcon m_icon;
  private BufferedImage m_Img = null;
  private static File m_fCurrentDirectory = null;
  private static NumberFormat m_percentformat = new DecimalFormat("#,##0.##%");
  private JPanel jPanel1;
  private JPanel jPanel2;
  private JLabel m_jImage;
  private JLabel m_jPercent;
  private JScrollPane m_jScr;
  private JButton m_jbtnclose;
  private JButton m_jbtnopen;
  private JButton m_jbtnzoomin;
  private JButton m_jbtnzoomout;

  public JImageEditor()
  {
    initComponents();
    this.m_Img = null;
    this.m_maxsize = null;
    this.m_icon = new ZoomIcon();
    this.m_jImage.setIcon(this.m_icon);
    this.m_jPercent.setText(m_percentformat.format(this.m_icon.getZoom()));
    privateSetEnabled(isEnabled());
  }

  public void setMaxDimensions(Dimension paramDimension)
  {
    this.m_maxsize = paramDimension;
  }

  public Dimension getMaxDimensions()
  {
    return this.m_maxsize;
  }

  public void setEnabled(boolean paramBoolean)
  {
    privateSetEnabled(paramBoolean);
    super.setEnabled(paramBoolean);
  }

  private void privateSetEnabled(boolean paramBoolean)
  {
    this.m_jbtnopen.setEnabled(paramBoolean);
    this.m_jbtnclose.setEnabled((paramBoolean) && (this.m_Img != null));
    this.m_jbtnzoomin.setEnabled((paramBoolean) && (this.m_Img != null));
    this.m_jbtnzoomout.setEnabled((paramBoolean) && (this.m_Img != null));
    this.m_jPercent.setEnabled((paramBoolean) && (this.m_Img != null));
    this.m_jScr.setEnabled((paramBoolean) && (this.m_Img != null));
  }

  public void setImage(BufferedImage paramBufferedImage)
  {
    BufferedImage localBufferedImage = this.m_Img;
    this.m_Img = paramBufferedImage;
    this.m_icon.setIcon(this.m_Img == null ? null : new ImageIcon(this.m_Img));
    this.m_jPercent.setText(m_percentformat.format(this.m_icon.getZoom()));
    this.m_jImage.revalidate();
    this.m_jScr.revalidate();
    this.m_jScr.repaint();
    privateSetEnabled(isEnabled());
    firePropertyChange("image", localBufferedImage, this.m_Img);
  }

  public BufferedImage getImage()
  {
    return this.m_Img;
  }

  public double getZoom()
  {
    return this.m_icon.getZoom();
  }

  public void setZoom(double paramDouble)
  {
    double d = this.m_icon.getZoom();
    this.m_icon.setZoom(paramDouble);
    this.m_jPercent.setText(m_percentformat.format(this.m_icon.getZoom()));
    this.m_jImage.revalidate();
    this.m_jScr.revalidate();
    this.m_jScr.repaint();
    firePropertyChange("zoom", d, paramDouble);
  }

  public void incZoom()
  {
    double d = this.m_icon.getZoom();
    setZoom(d > 4.0D ? 8.0D : d * 2.0D);
  }

  public void decZoom()
  {
    double d = this.m_icon.getZoom();
    setZoom(d < 0.5D ? 0.25D : d / 2.0D);
  }

  public void doLoad()
  {
    JFileChooser localJFileChooser = new JFileChooser(m_fCurrentDirectory);
    localJFileChooser.addChoosableFileFilter(new ExtensionsFilter(LocalRes.getIntString("label.imagefiles"), new String[] { "png", "gif", "jpg", "jpeg", "bmp" }));
    if (localJFileChooser.showOpenDialog(this) == 0)
      try
      {
        BufferedImage localBufferedImage = ImageIO.read(localJFileChooser.getSelectedFile());
        if (localBufferedImage != null)
        {
          if ((this.m_maxsize != null) && ((localBufferedImage.getHeight() > this.m_maxsize.height) || (localBufferedImage.getWidth() > this.m_maxsize.width)) && (JOptionPane.showConfirmDialog(this, LocalRes.getIntString("message.resizeimage"), LocalRes.getIntString("title.editor"), 0, 3) == 0))
            localBufferedImage = resizeImage(localBufferedImage);
          setImage(localBufferedImage);
          m_fCurrentDirectory = localJFileChooser.getCurrentDirectory();
        }
      }
      catch (IOException localIOException)
      {
      }
  }

  private BufferedImage resizeImage(BufferedImage paramBufferedImage)
  {
    int i = paramBufferedImage.getHeight();
    int j = paramBufferedImage.getWidth();
    if (i > this.m_maxsize.height)
    {
      j = j * this.m_maxsize.height / i;
      i = this.m_maxsize.height;
    }
    if (j > this.m_maxsize.width)
    {
      i = i * this.m_maxsize.width / j;
      j = this.m_maxsize.width;
    }
    BufferedImage localBufferedImage = new BufferedImage(j, i, 6);
    double d1 = j / paramBufferedImage.getWidth(null);
    double d2 = i / paramBufferedImage.getHeight(null);
    Graphics2D localGraphics2D = localBufferedImage.createGraphics();
    localGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    localGraphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    localGraphics2D.setColor(new Color(0, 0, 0, 0));
    localGraphics2D.fillRect(0, 0, j, i);
    if (d1 < d2)
      localGraphics2D.drawImage(paramBufferedImage, 0, (int)((i - paramBufferedImage.getHeight(null) * d1) / 2.0D), j, (int)(paramBufferedImage.getHeight(null) * d1), null);
    else
      localGraphics2D.drawImage(paramBufferedImage, (int)((j - paramBufferedImage.getWidth(null) * d2) / 2.0D), 0, (int)(paramBufferedImage.getWidth(null) * d2), i, null);
    localGraphics2D.dispose();
    return localBufferedImage;
  }

  private void initComponents()
  {
    this.m_jScr = new JScrollPane();
    this.m_jImage = new JLabel();
    this.jPanel1 = new JPanel();
    this.jPanel2 = new JPanel();
    this.m_jbtnopen = new JButton();
    this.m_jbtnclose = new JButton();
    this.m_jbtnzoomin = new JButton();
    this.m_jPercent = new JLabel();
    this.m_jbtnzoomout = new JButton();
    setLayout(new BorderLayout());
    this.m_jImage.setHorizontalAlignment(0);
    this.m_jImage.setHorizontalTextPosition(0);
    this.m_jScr.setViewportView(this.m_jImage);
    add(this.m_jScr, "Center");
    this.jPanel1.setLayout(new BorderLayout());
    this.jPanel2.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
    this.jPanel2.setLayout(new GridLayout(0, 1, 0, 5));
    this.m_jbtnopen.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/fileopen.png")));
    this.m_jbtnopen.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JImageEditor.this.m_jbtnopenActionPerformed(paramActionEvent);
      }
    });
    this.jPanel2.add(this.m_jbtnopen);
    this.m_jbtnclose.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/fileclose.png")));
    this.m_jbtnclose.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JImageEditor.this.m_jbtncloseActionPerformed(paramActionEvent);
      }
    });
    this.jPanel2.add(this.m_jbtnclose);
    this.m_jbtnzoomin.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/viewmag+.png")));
    this.m_jbtnzoomin.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JImageEditor.this.m_jbtnzoominActionPerformed(paramActionEvent);
      }
    });
    this.jPanel2.add(this.m_jbtnzoomin);
    this.m_jPercent.setBackground(Color.white);
    this.m_jPercent.setHorizontalAlignment(4);
    this.m_jPercent.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(UIManager.getDefaults().getColor("Button.darkShadow")), BorderFactory.createEmptyBorder(1, 4, 1, 4)));
    this.m_jPercent.setOpaque(true);
    this.jPanel2.add(this.m_jPercent);
    this.m_jbtnzoomout.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/viewmag-.png")));
    this.m_jbtnzoomout.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JImageEditor.this.m_jbtnzoomoutActionPerformed(paramActionEvent);
      }
    });
    this.jPanel2.add(this.m_jbtnzoomout);
    this.jPanel1.add(this.jPanel2, "North");
    add(this.jPanel1, "After");
  }

  private void m_jbtnzoomoutActionPerformed(ActionEvent paramActionEvent)
  {
    decZoom();
  }

  private void m_jbtnzoominActionPerformed(ActionEvent paramActionEvent)
  {
    incZoom();
  }

  private void m_jbtncloseActionPerformed(ActionEvent paramActionEvent)
  {
    setImage(null);
  }

  private void m_jbtnopenActionPerformed(ActionEvent paramActionEvent)
  {
    doLoad();
  }

  private static class ExtensionsFilter extends FileFilter
  {
    private String message;
    private String[] extensions;

    public ExtensionsFilter(String paramString, String[] paramArrayOfString)
    {
      this.message = paramString;
      this.extensions = paramArrayOfString;
    }

    public boolean accept(File paramFile)
    {
      if (paramFile.isDirectory())
        return true;
      String str1 = paramFile.getName();
      int i = str1.lastIndexOf('.');
      if (i >= 0)
      {
        String str2 = str1.substring(i + 1);
        for (String str3 : this.extensions)
          if (str3.equalsIgnoreCase(str2))
            return true;
      }
      return false;
    }

    public String getDescription()
    {
      return this.message;
    }
  }

  private static class ZoomIcon
    implements Icon
  {
    private Icon ico = null;
    private double zoom = 1.0D;

    public int getIconHeight()
    {
      return this.ico == null ? 0 : (int)(this.zoom * this.ico.getIconHeight());
    }

    public int getIconWidth()
    {
      return this.ico == null ? 0 : (int)(this.zoom * this.ico.getIconWidth());
    }

    public void paintIcon(Component paramComponent, Graphics paramGraphics, int paramInt1, int paramInt2)
    {
      if (this.ico != null)
      {
        Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
        localGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        localGraphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        AffineTransform localAffineTransform = localGraphics2D.getTransform();
        localGraphics2D.transform(AffineTransform.getScaleInstance(this.zoom, this.zoom));
        this.ico.paintIcon(paramComponent, localGraphics2D, (int)(paramInt1 / this.zoom), (int)(paramInt2 / this.zoom));
        localGraphics2D.setTransform(localAffineTransform);
      }
    }

    public void setIcon(Icon paramIcon)
    {
      this.ico = paramIcon;
      this.zoom = 1.0D;
    }

    public void setZoom(double paramDouble)
    {
      this.zoom = paramDouble;
    }

    public double getZoom()
    {
      return this.zoom;
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.data.gui.JImageEditor
 * JD-Core Version:    0.6.0
 */